package com.bhs.sansong.ui.setting;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

import com.bhs.sansong.R;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.utils.SharedPreferencesUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

/**
 * 绑定收款账户
 */
public class AccountCreditedActivity extends BaseActivity implements View.OnClickListener {


    private TextView mTvTitle;
    private LinearLayout mLlAlipay;
    private TextView mTvType;
    private TextView mTvPhone;
    private ActivityResultLauncher<Intent> intentActivityResultLauncher;

    @Override
    public int initLayout() {
        return R.layout.activity_account_credited;
    }

    @Override
    public void initView() {

        mTvTitle = findViewById(R.id.tv_title);
        mLlAlipay = findViewById(R.id.ll_Alipay);
        mTvType = findViewById(R.id.tv_type);
        mTvPhone = findViewById(R.id.tv_phone);
        mLlAlipay.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mTvTitle.setText("收款账户");
        intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                //此处是跳转的result回调方法
                if (result.getData() != null) {
                    String phone = result.getData().getStringExtra("phone");
                    mTvPhone.setText(phone);
                    mTvType.setText("换绑");
                }
            }
        });
        String alipayAccount = SharedPreferencesUtils.getString(mActivity, "alipayAccount");
        if (!TextUtils.isEmpty(alipayAccount)) {
            mTvPhone.setText(alipayAccount);
            mTvType.setText("换绑");
        }
    }

    @Override
    public void onClick(View view) {
        boolean is_approve = SharedPreferencesUtils.getBoolean(mActivity, "is_approve", false);
        if (is_approve) {
            intentActivityResultLauncher.launch(new Intent(mActivity, AccountCreditedEditActivity.class));
        } else {
            new XPopup.Builder(mActivity)
                    .asConfirm("提示", "你还未实名认证，现在去认证",
                            "取消", "确定",
                            new OnConfirmListener() {
                                @SuppressLint("NotifyDataSetChanged")
                                @Override
                                public void onConfirm() {
                                    startActivity(new Intent(mActivity, RealNameAuthenticationActivity.class));
                                }
                            }, null, false)
                    .show();

        }
    }
}